/*  1:   */ package org.jetbrains.rpc;
/*  2:   */ 
/*  3:   */

import com.intellij.util.text.StringFactory;
import io.netty.buffer.ByteBuf;
import io.netty.util.CharsetUtil;

import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CoderResult;

/*  4:   */
/*  5:   */
/*  6:   */
/*  7:   */
/*  8:   */
/*  9:   */
/* 10:   */
/* 11:   */ 
/* 12:   */ public final class ChannelBufferToString
/* 13:   */ {
/* 14:   */   public static String readString(ByteBuf paramByteBuf)
/* 15:   */   {
/* 16:15 */     return charBufferToString(readIntoCharBuffer(null, paramByteBuf, paramByteBuf.readableBytes()));
/* 17:   */   }
/* 18:   */   
/* 19:   */   public static String readString(ByteBuf paramByteBuf, int paramInt)
/* 20:   */   {
/* 21:19 */     return charBufferToString(readIntoCharBuffer(null, paramByteBuf, paramInt));
/* 22:   */   }
/* 23:   */   
/* 24:   */   public static String charBufferToString(CharBuffer paramCharBuffer)
/* 25:   */   {
/* 26:23 */     char[] arrayOfChar = paramCharBuffer.array();
/* 27:24 */     if (arrayOfChar.length == paramCharBuffer.position()) {
/* 28:25 */       return StringFactory.createShared(arrayOfChar);
/* 29:   */     }
/* 30:28 */     return paramCharBuffer.flip().toString();
/* 31:   */   }
/* 32:   */   
/* 33:   */   public static CharBuffer readIntoCharBuffer(CharBuffer paramCharBuffer, ByteBuf paramByteBuf, int paramInt)
/* 34:   */   {
/* 35:33 */     CharsetDecoder localCharsetDecoder = CharsetUtil.getDecoder(CharsetUtil.UTF_8);
/* 36:34 */     ByteBuffer localByteBuffer = paramByteBuf.nioBuffer(paramByteBuf.readerIndex(), paramInt);
/* 37:35 */     if (paramCharBuffer == null) {
/* 38:36 */       paramCharBuffer = CharBuffer.allocate((int)(localByteBuffer.remaining() * localCharsetDecoder.maxCharsPerByte()));
/* 39:   */     }
/* 40:   */     try
/* 41:   */     {
/* 42:39 */       CoderResult localCoderResult = localCharsetDecoder.decode(localByteBuffer, paramCharBuffer, true);
/* 43:40 */       if (!localCoderResult.isUnderflow()) {
/* 44:41 */         localCoderResult.throwException();
/* 45:   */       }
/* 46:43 */       localCoderResult = localCharsetDecoder.flush(paramCharBuffer);
/* 47:44 */       if (!localCoderResult.isUnderflow()) {
/* 48:45 */         localCoderResult.throwException();
/* 49:   */       }
/* 50:   */     }
/* 51:   */     catch (CharacterCodingException localCharacterCodingException)
/* 52:   */     {
/* 53:49 */       throw new IllegalStateException(localCharacterCodingException);
/* 54:   */     }
/* 55:52 */     paramByteBuf.skipBytes(paramInt);
/* 56:53 */     return paramCharBuffer;
/* 57:   */   }
/* 58:   */ }



/* Location:           E:\idea\

 * Qualified Name:     org.jetbrains.rpc.ChannelBufferToString

 * JD-Core Version:    0.7.0.1

 */